package com.langchain4j.config;

import com.langchain4j.tool.CalcTool;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.input.structured.StructuredPrompt;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiImageModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Flux;

@Configuration
public class AIConfig {

    @Bean
    public OpenAiStreamingChatModel openAiStreamingChatModel(){
        /*return OpenAiChatModel.builder()
                .apiKey("sk-a189a0b6e45d4dcd9b434b23ff78eeb1")
                .modelName("deepseek-v3.1")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .logResponses(true)
                .build();*/
        return OpenAiStreamingChatModel.builder()
                .apiKey("sk-a189a0b6e45d4dcd9b434b23ff78eeb1")
                .modelName("deepseek-v3.1")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    @Bean
    public OpenAiChatModel chatModel(){
        return OpenAiChatModel.builder()
                .apiKey("sk-a189a0b6e45d4dcd9b434b23ff78eeb1")
                .modelName("deepseek-v3.1")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    @Bean
    public OpenAiImageModel openAiImageModel(){
        return OpenAiImageModel.builder()
                .apiKey("sk-a189a0b6e45d4dcd9b434b23ff78eeb1")
                .modelName("deepseek-v3.1")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    public interface IATest{
        String chat1(@MemoryId int userId, @UserMessage String message);
        Flux<String> chat(@MemoryId int userId, @UserMessage String message);
        @SystemMessage("你好啊，{{userId}}")
        @UserMessage("请回答以下问题:{{aaa}}")
        Flux<String> chat2(@MemoryId @V("userId") int userId, @V("aaa") String message);
        // 自定义结构化提示词
        @SystemMessage("你是一位专业的中国{{legal}}法律顾问，只回答与中国{{legal}}法律相关的问题，输出限制:其他领域的问题禁止回直接返回 抱歉，我只能回答中国{{legal}}法律相关问题")
        Flux<String> chat3(@MemoryId int userId, @UserMessage LegalPrompt legalPrompt, @V("legal") String legal);
    }

    @Bean
    public IATest iaTest(OpenAiStreamingChatModel  openAiStreamingChatModel,OpenAiChatModel chatModel){
        return AiServices.builder(IATest.class)
                .streamingChatModel(openAiStreamingChatModel)
//                .chatModel(chatModel)
                .tools(new CalcTool())// 工具调用
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(100))
                .build();
    }


    // 自定义结构化提示词
    @Data
    @StructuredPrompt("根据中国{{legal}}法律,解答以下问题:{{question}}，如果问题与中国{{legal}}法律无关，请回答: 抱歉，我只能回答中国{{legal}}法律相关问题")
    public static class LegalPrompt {
        public String legal;
        public String question;
    }
}
